package simulaco.web.action;

import gminet.infra.business.BusinessException;
import gminet.infra.web.AccessException;
import gminet.infra.web.ConfigPropertiesWEB;
import gminet.infra.web.SessionAccess;

import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import simulaco.dto.Acao;
import simulaco.dto.Usuario;
import simulaco.dto.UsuarioPerfil;


public class WebAction implements Serializable {

    private static final long serialVersionUID = 6688463139467955200L;

    private static String CONTENT_REAL_PATH = null;

    private Acao acao;
    
    private boolean confirmacaoAcao;

    public WebAction() {
    }

    public static boolean contentRealPathInitialized() {

        return CONTENT_REAL_PATH != null;
    }

    public static void inicializarContentRealPath(String contentRealPath) {

        if (CONTENT_REAL_PATH == null) {
            CONTENT_REAL_PATH = contentRealPath;
        }
    }
    
    public static String getContentRealPath() {
        return WebAction.CONTENT_REAL_PATH;
    }

    public HttpSession getSession() {

        return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public Usuario getUsuarioLocal() {

        return (Usuario) SessionAccess.getAccessObject(getSession());
    }

    public boolean isLogado() {

        return getUsuarioLocal() != null;
    }

    public boolean isAdministrador() {

        return isLogado() && getUsuarioLocal().getPerfil().getCodigo() >= UsuarioPerfil.ADMINISTRADOR.getCodigo();
    }

    public void setUsuarioLocal(Usuario usuario) {

        SessionAccess.setAccessObject(usuario, getSession());
    }

    public void releaseUsuarioLocal() {

        SessionAccess.releaseAccessObject(getSession());
    }

    public void releaseSession() {

        getSession().invalidate();
    }

    public void verificarLogin() throws AccessException {

        if (getUsuarioLocal() == null) {
            throw new AccessException("Usuário não logado no sistema.");
        }
    }

    public void verificarLogin(UsuarioPerfil usuarioPerfil) throws AccessException {

        verificarLogin();
        if (getUsuarioLocal().getPerfil().getCodigo() < usuarioPerfil.getCodigo()) {
            throw new AccessException("Usuário sem acesso a página. Acesse como " + usuarioPerfil.getNome() + ".");
        }
    }
    
    public Date getDate() {
        
        return new Date();
    }

    public void addMessage(String clientId, String strMessage) {

        FacesMessage message = new FacesMessage(strMessage);
        message.setSeverity(FacesMessage.SEVERITY_INFO);
        message.setDetail(strMessage);
        FacesContext.getCurrentInstance().addMessage(clientId, message);
        //
        RequestContext context = RequestContext.getCurrentInstance();
        if (context != null) {
            context.addCallbackParam("hasMessage", true);
        }
    }

    public void addBusinessException(String clientId, BusinessException exception) {
        
        FacesMessage message = new FacesMessage(exception.getMessage());
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        StringBuffer detail = new StringBuffer();
        detail.append("");
        Throwable cause = exception.getCause();
        while (cause != null) {
            detail.append(cause.getMessage());
            cause = cause.getCause();
        }
        message.setDetail(detail.toString());
        FacesContext.getCurrentInstance().addMessage(clientId, message);
        //
        RequestContext context = RequestContext.getCurrentInstance();
        if (context != null) {
            context.addCallbackParam("hasBusinessException", true);
        }
    }
    
    public Acao getAcao() {
        if (acao == null) {
            acao = Acao.INDEFINIDO;
        }
        return acao;
    }

    public void setConfirmacaoAcao(boolean confirmAction) {
        this.confirmacaoAcao = confirmAction;
    }
    
    public boolean isConfirmacaoAcao() {
        return confirmacaoAcao;
    }
    
    public boolean isAcaoIncluir() {
        return getAcao().equals(Acao.INCLUIR);
    }

    public void setAcaoIncluir(boolean acaoIncluir) {

        if (acaoIncluir) {
            setAcao(Acao.INCLUIR);
        }
    }

    public boolean isAcaoAlterar() {
        return getAcao().equals(Acao.ALTERAR);
    }

    public void setAcaoAlterar(boolean acaoAlterar) {
        if (acaoAlterar) {
            setAcao(Acao.ALTERAR);
        }
    }

    public boolean isAcaoExcluir() {
        return getAcao().equals(Acao.EXCLUIR);
    }

    public void setAcaoExcluir(boolean acaoExcluir) {
        if (acaoExcluir) {
            setAcao(Acao.EXCLUIR);
        }
    }

    public boolean isAcaoAtivar() {
        return getAcao().equals(Acao.ATIVAR);
    }

    public void setAcaoAtivar(boolean acaoAtivar) {
        if (acaoAtivar) {
            setAcao(Acao.ATIVAR);
        }
    }

    public boolean isAcaoDesativar() {
        return getAcao().equals(Acao.DESATIVAR);
    }

    public void setAcaoDesativar(boolean acaoDesativar) {
        if (acaoDesativar) {
            setAcao(Acao.DESATIVAR);
        }
    }

    public void setAcao(Acao actionPerfil) {
        this.acao = actionPerfil;
    }
    
    public String getAccessEmail() {
        
        return ConfigPropertiesWEB.getAccessEmail();
    }

    public String getAccessPassword() {
        
        return ConfigPropertiesWEB.getAccessPassword();
    }

}
